﻿using EcoSystem.Enums;
using System.Reflection;
using System;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Reflection;
using Volo.Abp.Localization;
using EcoSystem.Localization;
using System.Linq;

namespace EcoSystem.Permissions;

public static class EcoSystemPermissions
{
    public const string GroupName = "EcoSystem";


    /// <summary>
    /// 菜单管理
    /// </summary>
    public static class SystemMenu
    {
        public const string Default = GroupName + ".SystemMenu";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 数据字典
    /// </summary>
    public static class Dictionary
    {
        public const string Default = GroupName + ".Dictionary";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 配置分类
    /// </summary>
    public static class ConfigCategory
    {
        public const string Default = GroupName + ".ConfigCategory";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 配置定义
    /// </summary>
    public static class ConfigDefine
    {
        public const string Default = GroupName + ".ConfigDefine";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }


    /// <summary>
    /// 配置管理
    /// </summary>
    public static class ConfigSetting
    {
        public const string Default = GroupName + ".ConfigSetting";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 取号配置
    /// </summary>
    public static class TakeConfig
    {
        public const string Default = GroupName + ".TakeConfig";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }


    /// <summary>
    /// 系统用户管理
    /// </summary>
    public static class SystemUser
    {
        public const string Default = GroupName + ".SystemUser";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 企业管理
    /// </summary>
    public static class Enterprise
    {
        public const string Default = GroupName + ".Enterprise";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 组织架构
    /// </summary>
    public static class Organization
    {
        public const string Default = GroupName + ".Organization";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    public static class DepartmentUser
    {
        public const string Default = GroupName + ".DepartmentUser";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 行政区域
    /// </summary>
    public static class Region
    {
        public const string Default = GroupName + ".Region";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 资源块管理
    /// </summary>
    public static class Bucket
    {
        public const string Default = GroupName + ".Bucket";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 资源文件管理
    /// </summary>
    public static class FileResource
    {
        public const string Default = GroupName + ".FileResource";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }  /// <summary>
       /// 资源目录管理
       /// </summary>
    public static class DirResource
    {
        public const string Default = GroupName + ".DirResource";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 附件配置
    /// </summary>
    public static class AttachmentConfig
    {
        public const string Default = GroupName + ".AttachmentConfig";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 附件管理
    /// </summary>
    public static class Attachment
    {
        public const string Default = GroupName + ".Attachment";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
        public const string Upload = Default + ".Upload";
    }

    /// <summary>
    /// 专家库管理
    /// </summary>
    public static class ExpertLibrary
    {
        public const string Default = GroupName + ".ExpertLibrary";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 专家目录库
    /// </summary>
    public static class ExpertCatalog
    {
        public const string Default = GroupName + ".ExpertCatalog";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 专家审核管理
    /// </summary>
    public static class ExpertInventoryAudit
    {
        public const string Default = GroupName + ".ExpertInventoryAudit";
        public const string View = Default + ".View";
        public const string Audit = Default + ".Audit";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 专家申请管理
    /// </summary>
    public static class ExpertApply
    {
        public const string Default = GroupName + ".ExpertApply";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 专家抽取管理
    /// </summary>
    public static class ExpertExtractTask
    {
        public const string Default = GroupName + ".ExpertExtractTask";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }
    /// <summary>
    /// 专家公示管理
    /// </summary>
    public static class ExpertExtractPublicity
    {
        public const string Default = GroupName + ".ExpertExtractPublicity";
        public const string View = Default + ".View";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 报表分类配置
    /// </summary>
    public static class ReportCategoryConfig
    {
        public const string Default = GroupName + ".ReportCategoryConfig";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }


    /// <summary>
    /// API接口配置
    /// </summary>
    public static class HttpApiConfig
    {
        public const string Default = GroupName + ".HttpApiConfig";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }


    /// <summary>
    /// 通用报表配置
    /// </summary>
    public static class ReportConfig
    {
        public const string Default = GroupName + ".ReportConfig";
        public const string Create = Default + ".Create";
        public const string Update = Default + ".Update";
        public const string Delete = Default + ".Delete";
    }

    /// <summary>
    /// 统计报表
    /// </summary>
    public static class StatisticalReport
    {
        public const string Default = GroupName + ".StatisticalReport";
        public const string ApplyCensus = Default + ".ApplyCensus";
        public const string ExpertCensus = Default + ".ExpertCensus";
        public const string InventoryAuditCensus = Default + ".InventoryAuditCensus";
        public const string ExpertUseCensus = Default + ".ExpertUseCensus";
    }

    /// <summary>
    /// 扩展权限类型属性
    /// </summary>
    /// <param name="permissions"></param>
    /// <param name="type"></param>
    /// <param name="icon"></param>
    /// <returns></returns>
    public static PermissionDefinition WithType(this PermissionDefinition permissions, PermissionType type, string icon = null)
    {
        permissions.WithProperty("Type", type);
        if (!icon.IsNullOrWhiteSpace())
        {
            permissions.WithProperty("Icon", icon);
        }
        return permissions;
    }

    /// <summary>
    /// 添加模块权限
    /// </summary>
    /// <param name="group"></param>
    /// <param name="type"></param>
    public static void AddModulePermission(this PermissionGroupDefinition group, Type type)
    {
        var items = type.GetFields(BindingFlags.Static | BindingFlags.Public);
        var menu = items.FirstOrDefault(x => x.Name == "Default");
        var module = group.AddPermission(menu.GetRawConstantValue().ToString(), L("Menu:" + type.Name)).WithType(PermissionType.Module);
        items = items.Where(x => x.Name != "Default").ToArray();
        foreach (var item in items)
        {
            module.AddChild(item.GetRawConstantValue().ToString(), L(item.Name)).WithType(PermissionType.Action);
        }
    }

    public static string[] GetAll()
    {
        return ReflectionHelper.GetPublicConstantsRecursively(typeof(EcoSystemPermissions));
    }

    private static LocalizableString L(string name)
    {
        return LocalizableString.Create<EcoSystemResource>(name);
    }
}
